package com.backend.werewolfkillingbackend.contorller;

import com.backend.werewolfkillingbackend.common.R;
import com.backend.werewolfkillingbackend.pojo.Player;
import com.backend.werewolfkillingbackend.pojo.Room;
import com.backend.werewolfkillingbackend.pojo.User;
import com.backend.werewolfkillingbackend.service.RoomService;
import com.backend.werewolfkillingbackend.util.JsonUtil;
import com.backend.werewolfkillingbackend.util.NumberGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
@RequestMapping("/api/private/v1")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping("/search_room/{id}")
    public R<Room> searchRoom(@PathVariable String id) {
        Room room = roomService.getById(id);

        return R.success(room, "获取成功");
    }

    @PostMapping("/create_room")
    public R<Room> createRoom(@RequestBody User user) throws JsonProcessingException {
        Room room = new Room();
        room.setRoomName(NumberGenerator.generateName());
        room.setRoomOwner(user.getUsername());
        room.setGameStatus(0);
        room.setId(Integer.parseInt(NumberGenerator.generateNumber()));
        room.setPlayerCount(1);
        room.setPlayerList(JsonUtil.parseListToJson(new ArrayList<Player>()));

        roomService.save(room);
        return R.success(room, "创建成功");
    }
}
